package com.dogkr.server.web;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.ResultPath;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

import com.dogkr.common.Constants;
import com.dogkr.common.ServerAction;
import com.dogkr.server.entity.User;
import com.dogkr.server.entity.UserPet;
import com.dogkr.server.service.UserPetService;


// 设置Struts2注解
@ParentPackage(value = "login-default")
@Namespace(value = "/pet")
@ResultPath("/WEB-INF/jsps")
// 页面文件空间
@Results({
	@Result(name = "main", location = "main.jsp"),
	@Result(name = "edit", location = "edit.jsp")
})
public class UserPetAction extends ServerAction {
	private static final long serialVersionUID = 1L;
	
	@Autowired
	private UserPetService userPetService;
	
	private UserPet pet;
	
	@Action(value = "edit")
	public String edit() {
		put("fileSize", Constants.UPLOAD_FILE_SIZE);
		return "edit";
	}
	
	@Action(value="list")
	public void getList() {
		User user = this.getLoginUser();
		outJson(userPetService.selectUserPetByUserKey(user.getId()));
	}
	
	@Action(value="get")
	public void getUserPet() {
		outJson(userPetService.selectByPrimaryKey(id));
	}
	
	@Action(value="save")
	public void savePet() {
		pet.setUserId(this.getLoginUser().getId());
		if(pet.getId() == null) {
			userPetService.insert(pet);
		} else {
			userPetService.updateByPrimaryKeySelectiveByUser(pet);
		}
		outJson(res);
	}

	public void setPet(UserPet pet) {
		this.pet = pet;
	}

	public UserPet getPet() {
		return pet;
	}
}
